package com.cjd.view

import android.content.Context
import android.content.res.TypedArray
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.cjd.common.R
import com.cjd.common.utils.LogUtils
import kotlin.math.roundToInt

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2020-03-04
 * description 弧形背景
 */
class ArcView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var arcHeight = 0
    private var maxArcHeight = 0
    private var paint: Paint? = null
    private var rect: RectF? = null
    private var path: Path? = null

    private var viewWidth = 0
    private var viewHeight: Int = 0
    private var paintColor: Int = Color.BLUE
    private var bgColor: Int = Color.WHITE

    init {
        if (attrs != null) {
            val ta: TypedArray =
                context.obtainStyledAttributes(attrs, R.styleable.ArcView)
            bgColor = ta.getColor(R.styleable.ArcView_arc_active_color, paintColor)
            paintColor = ta.getColor(R.styleable.ArcView_arc_background_color, bgColor)
            maxArcHeight =
                ta.getDimensionPixelOffset(R.styleable.ArcView_max_arc_height, dp2px(100f))
            ta.recycle()
        } else
            maxArcHeight = dp2px(100f)
        arcHeight = maxArcHeight
        paint = Paint()
        paint?.isAntiAlias = true
        paint?.style = Paint.Style.FILL
        paint?.color = paintColor

        rect = RectF()
        path = Path()
        setBackgroundColor(bgColor)
    }


    private fun dp2px(dpValue: Float): Int {
        val scale = resources.displayMetrics.density
        return (scale * dpValue + 0.5f).toInt()
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED) {
            viewWidth = MeasureSpec.getSize(widthMeasureSpec)
        }
        if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.UNSPECIFIED) {
            viewHeight = MeasureSpec.getSize(heightMeasureSpec)
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        path?.let {
            it.reset()
            it.moveTo(0f, 0f)
            it.lineTo(0f, viewHeight.toFloat())
            it.quadTo(
                viewWidth / 2f,
                viewHeight - arcHeight.toFloat(),
                viewWidth.toFloat(),
                viewHeight.toFloat()
            )
            it.lineTo(width.toFloat(), 0f)
            it.close()
            it.computeBounds(rect, true)
            canvas.drawPath(it, paint!!)
        }
    }

    fun setArcHeight(arcHeight: Int) {
        if (arcHeight > maxArcHeight)
            this.arcHeight = maxArcHeight
        else
            this.arcHeight = arcHeight
        if (this.arcHeight < 0)
            this.arcHeight = 0

        if (this.arcHeight >= 0)
            postInvalidate()
    }

    /**
     * onDraw() 后才能正确获取 高度
     * return 0 默认值0表示 未初始化绘制 / 非四边形
     */
    fun getBezierArcMaxHeight(): Int {
        rect?.let {
            //根据原理可得四边形（三点情况下 AB BC 长度相等） AB BC 的中间点 D E   F（弧度最高点） = viewHeight / 2
            if ((it.left + it.bottom) == (it.top + it.bottom)) {//四边形
                val rect = (it.left + it.bottom)
                return if (rect <= 0) 0 else (rect / 2).roundToInt()
            }
        }
        return 0
    }

    fun getArcHeight(): Int {
        return this.arcHeight
    }

    fun setMaxArcHeight() {
        setArcHeight(maxArcHeight)
    }

    fun getMaxArcHeight(): Int {
        return maxArcHeight
    }
}